﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedeSocial.DataAccess;
using System.Collections.ObjectModel;
using RedeSocial.ViewModels.Enums;
using RedeSocial.ViewModels.Interfaces;
using RedeSocial.Errors;

namespace RedeSocial.ViewModels
{
	public class CreateUserViewModel : BaseViewModel, ICreateUserViewModel
	{

		#region fields...
		private string _email = string.Empty;
		private string _name = string.Empty;
		private Gender _gender = Gender.Unknown;
		private Nullable<System.DateTime> _birthday;
		private string _password = string.Empty;
		private string _confirmPassword = string.Empty;
		private Country _country;
		private Language _defaultLanguage;
		private bool _agreeWithTOS;
		private bool _isNew = true;

		#endregion
		#region properties...
		public string Email
		{
			get { return _email; }
			set
			{
				_email = value;
				NotifyPropertyChange("Email");
			}
		}
		public string Name
		{
			get { return _name; }
			set
			{
				_name = value;
				NotifyPropertyChange("Name");
			}
		}

		public Gender Gender
		{
			get { return _gender; }
			set
			{
				_gender = value;
				NotifyPropertyChange("Gender");
			}
		}
		public Nullable<System.DateTime> Birthday
		{
			get
			{
				return _birthday;
			}
			set
			{
				_birthday = value;
				NotifyPropertyChange("Birthday");
			}
		}

		public string Password
		{
			get { return _password; }
			set
			{
				_password = value;
				NotifyPropertyChange("Password");
			}
		}
		public string ConfirmPassword
		{
			get { return _confirmPassword; }
			set
			{
				_confirmPassword = value;
				NotifyPropertyChange("ConfirmPassword");
			}
		}

		public Country Country
		{
			get { return _country; }
			set
			{
				_country = value;
				NotifyPropertyChange("Country");
			}
		}

		public bool AgreeWithTOS
		{
			get { return _agreeWithTOS; }
			set
			{
				_agreeWithTOS = value;
				NotifyPropertyChange("AgreeWithTOS");
			}
		}
		public Language DefaultLanguage
		{
			get { return _defaultLanguage; }
			set
			{
				_defaultLanguage = value;
				NotifyPropertyChange("DefaultLanguage");
			}
		}

		public bool IsNew
		{
			get { return _isNew; }
			set
			{
				_isNew = value;
				NotifyPropertyChange("IsNew");
			}
		}
		#endregion
		#region methods...
		public Session CreateUser()
		{
			if (!IsValid())
				return null;

			Session session = null;
			string encPwd = Security.Encrypt(Password);
			using (var dataContext = new RedeSocialEntities())
			{
				dataContext.Countries.Attach(Country);
				User user = dataContext.Users.Include("Country").Include("File").Where(u => u.Email.Equals(Email, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
				File file;
				if (Gender == Enums.Gender.Male)
					file = dataContext.Files.Where(f => f.Description.Equals("GenericMan")).FirstOrDefault();
				else
					file = dataContext.Files.Where(f => f.Description.Equals("GenericWoman")).FirstOrDefault();
				if (user != null)
				{
					throw new UserAlreadyExistException();
				}
				user = new User()
				{
					Birthday = Birthday,
					Country = Country,
					Email = Email,
					Gender = (int)Gender,
					IsAdmin = false,
					Language = DefaultLanguage,
					Name = Name,
					Password = encPwd,
					UserState = (int)UserState.Enabled,
					File = file
				};
				dataContext.Users.Add(user);
				session = new Session() { ConnectedOn = DateTime.Now, LastAccess = DateTime.Now, User = user };
				dataContext.Sessions.Add(session);
				dataContext.SaveChanges();
			}
			return session;
		}
		public User UpdateUser(User user)
		{
			if (!IsValid())
				return null;

			Session session = null;
			string encPwd = Security.Encrypt(Password);
			using (var dataContext = new RedeSocialEntities())
			{
				dataContext.Countries.Attach(Country);
				dataContext.Users.Attach(user);
				if (user == null)
				{
					throw new UserDoesNotExistException();
				}

				user.Birthday = Birthday;
				user.Country = Country;
				user.Gender = (int)Gender;
				user.Name = Name;
				if (!string.IsNullOrEmpty(Password))
				{
					user.Password = encPwd;
				}
				dataContext.SaveChanges();
			}
			return user;
		}
		#endregion
	}
}
